package com.sqm.timecard;

import java.util.*;
import java.io.*;
import org.apache.log4j.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */ 

public class Config
{

    private static Logger logger = Logger.getLogger( Config.class );
    private static final String CONFIG_FILE = "config/config.properties"; 
    private static Properties _pros = null;

    static
    {
        init(CONFIG_FILE);
    }

    private static void init( String fileName )
    {
        try
        {
            _pros = new Properties();
            if( fileName == null )
                fileName = CONFIG_FILE;
            _pros.load(new FileInputStream(fileName));
        }
        catch ( Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public static String getProperty( String key )
    {
        return  _pros.getProperty( key );
    }
    
    public static String getProperty( String key,String defaultValue )
    {
        return  _pros.getProperty( key,defaultValue );
    }

    public static int getInt( String key )
    {
        return Integer.parseInt(_pros.getProperty( key ));
    }
    
    public static boolean getBoolean( String key )
    {
        return Boolean.parseBoolean(_pros.getProperty( key ));
    }
    
    public static void store()
    {
        OutputStream os = null;
        try
        {
            os = new BufferedOutputStream(new FileOutputStream(CONFIG_FILE));
            _pros.store(os, "Stroe the parameters user entered");
        } catch (Exception e)
        {
            logger.error("DON'T WORRY THIS MESSAGE: " + e);
        } finally
        {
            try
            {
                if (os != null)
                    os.close();
            } catch (IOException e1)
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    }
    
	public static Calendar adjustStandardTime(Calendar cal,String type)
    {
        int hour;         
        int minute;
        if (type.equalsIgnoreCase("checkIn"))
        {
            hour = Config.getInt("checkInTime.hour");
            minute = Config.getInt("checkInTime.minute");
        }
        else
        {
            hour = Config.getInt("takeOffTime.hour");
            minute = Config.getInt("takeOffTime.minute");
        }
        cal.set(Calendar.HOUR_OF_DAY,hour);
        cal.set(Calendar.MINUTE,minute);
        cal.set(Calendar.SECOND, 0);
        return cal;
    }
  
}
